import QtQuick 1.0
import "content"

Column{
    id: screen
    width: 360
    height: 360

    //spacing: 30

    property int frameMargin: 10
    property int rowHeight:   60
    property int margin: 20
    property string brgndColour: "#469e63"

    Rectangle {
        id: cataloguePane
        width: parent.width
        height: parent.height - optionbar.height - screen.spacing
        color: screen.brgndColour

        BusyIndicator {
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            on: listView.model.isApplicationBusy
        }

        ListView {
            id: listView
            anchors.fill: parent
            anchors.margins: screen.margin
            spacing: screen.margin

            Component {
                id: drmListDelegate

                Item {
                    id: productRow
                    width: listView.width; height: screen.rowHeight
                    clip: true

                    Row {
                        spacing: screen.frameMargin

                        Image {
                            //NOTE image provider caches data, the side effect is updated image is not shown
                            source: (isActivated === true) ? "image://imagestatusprovider/"+index : //persistent , allow caching
                            //else disable caching
                            // workaround for http://bugreports.qt.nokia.com/browse/QTBUG-14900
                            "image://imagestatusprovider/"+index+
                            "ignoringpart" + Qt.formatTime(new Date(), "hhmmsszzz")
                            width: screen.rowHeight
                            height: screen.rowHeight

                            MouseArea {
                                anchors.fill:parent
                                onClicked: {
                                    console.log("onClicked")
                                    if(isDataKnown === true && isActivated === false){
                                        drmListModel.buy(index)
                                    }
                                }
                            }
                        }

                        BusyIndicator {
                            anchors.verticalCenter: parent.verticalCenter
                            width: screen.rowHeight / 2
                            height: screen.rowHeight / 2
                            on: isBusy
                        }

                        Text {
                            anchors.verticalCenter: parent.verticalCenter
                            text: (isDataKnown === true) ?
                                      (productid + "; " +reqresult + "; " +
                                       tile + "; " +
                                       shortDescr  + "; " +
                                       longDescr  + "; " +
                                       price)
                                    : productid + "; <UNKNOWN>"
                        }
                    }
                }
            }

            model: drmListModel
            delegate: drmListDelegate
        }
    }

    Rectangle{
        id: optionbar
        width: parent.width
        height:
            ((restoreBtn.width + purgeBtn.width +
                userInfoBtn.width + exitBtn.width +
                    screen.margin * 6) <= width) ? restoreBtn.height + screen.margin :
                                             (restoreBtn.height + screen.margin) * 2
        gradient: Gradient {
            GradientStop {
                position: 0.00;
                color: screen.brgndColour;
            }
            GradientStop {
                position: 1.00;
                color: "black";
            }
        }

        Flow{
            spacing: screen.margin
            width: parent.width

            Button {
                id: restoreBtn
                text: "Restore"
                onClicked: listView.model.restoreProducts()
            }

            Button {
                id: purgeBtn
                text: "Purge"
                onClicked: listView.model.purgeProducts()
            }

            Button {
                id: userInfoBtn
                text: "User info"
                onClicked: listView.model.getUserInfo()
            }

            Button {
                id: exitBtn
                text: "Exit"
                onClicked: Qt.quit()
            }
        }
    }
}
